//
//  DemoTableControllerViewController.m
//  FPPopoverDemo
//
//  Created by Alvise Susmel on 4/13/12.
//  Copyright (c) 2012 Fifty Pixels Ltd. All rights reserved.
//

#import "DemoTableController.h"
#import "KeyObject.h"
#import "DataManager.h"
#import "PopupView.h"

@interface DemoTableController ()

@end

@implementation DemoTableController

- (id)initWithStyle:(UITableViewStyle)style items:(NSArray *)array tag:(int)tag;
{
    self = [super initWithStyle:style];
    if(self)
    {
        items = array;
        btntag = tag;
    }
    
    self.tableView.width = 200;
    [self.tableView setSeparatorColor:[UIColor colorWithRed:43/255.0 green:11/255.0 blue:0/255.0 alpha:1]];
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0)
    {
        self.automaticallyAdjustsScrollViewInsets=NO;
    }
    self.view.backgroundColor=[UIColor colorWithRed:210/255.0 green:189/255.0 blue:149/255.0 alpha:1];
  //  [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(pass:) name:@"pass" object:nil];
  //  self.title = @"Popover Tit";
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    cell.backgroundColor = [UIColor colorWithRed:210/255.0 green:189/255.0 blue:149/255.0 alpha:1];
    cell.textLabel.textColor = [UIColor colorWithRed:43/255.0 green:11/255.0 blue:0/255.0 alpha:1];
    cell.textLabel.adjustsFontSizeToFitWidth=YES;
    if (btntag==102) {
        ShopObject* shop = [items objectAtIndex:indexPath.row];
        cell.textLabel.text = shop.shopName;
    }
    else if (btntag<10) {
        KeyObject *key = [items objectAtIndex:indexPath.row];
        cell.textLabel.text = key.keyName;
    }
    else if (btntag >200){
        //订货统计里使用的过滤条件
        if (btntag==301) {
            ShopObject *shopobj = [[DataManager sharedManager]getCurrentShop];
            NSMutableArray *array = [[NSMutableArray alloc]init];
            
            NSString *allName = ((KeyObject *)[items objectAtIndex:0]).keyName;
            
            NSDictionary *result = [[DataManager sharedManager]getFavoriteCount:shopobj.shopID all:allName];
            for (int i = 0; i<items.count; i++) {
                KeyObject *keyobj = [items objectAtIndex:i];
                
                int favnum = 0;
                
                id favCount= [result objectForKey:keyobj.keyName];
                favnum = [favCount intValue];
                
                NSString *str = [NSString stringWithFormat:@"%@(%d)",keyobj.keyName,favnum];
                [array addObject:str];
            }
            cell.textLabel.text = [array objectAtIndex:indexPath.row];
        }
        else
        {
        KeyObject *key = [items objectAtIndex:indexPath.row];
        cell.textLabel.text = key.keyName;
        }
    }
    
    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (btntag<10) {
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        [dic setObject:[NSNumber numberWithInt:btntag] forKey:@"category"];
        [dic setObject:[items objectAtIndex:indexPath.row] forKey:@"key"];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"index" object:dic];
    }
    else if(btntag==102){
        ShopObject* shop = [items objectAtIndex:indexPath.row];
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        
        if (shop.shopID) {
            [dic setObject:shop.shopID forKey:@"shopid"];
        }
        
        [dic setObject:shop.shopName forKey:@"title"];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"statRefreshShop" object:dic];
    }
    else if(btntag>200&&btntag<300)
    {
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        [dic setObject:[NSNumber numberWithInt:btntag] forKey:@"category"];
        [dic setObject:[items objectAtIndex:indexPath.row] forKey:@"key"];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"index2" object:dic];
    }
    else if(btntag>300)
    {
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        [dic setObject:[NSNumber numberWithInt:btntag] forKey:@"category"];
        [dic setObject:[items objectAtIndex:indexPath.row] forKey:@"key"];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"index3" object:dic];
    }
    
  //  [[NSNotificationCenter defaultCenter]postNotificationName:@"dismiss" object:nil];
}
/*
-(void)pass:(NSNotification *)sender
{
    items = sender.object;
}
*/
@end
